/**
 * @author Helio
 */
var SaleStatusDailyView = Ext.extend(BasicPanel,{
	
	initlize : function() {
		
		if(!this.detailsGrid) {
						
			this.detailsStore = new Ext.data.GroupingStore({
			    proxy: new Ext.data.MemoryProxy(this.detailsJson),
			    //remoteSort:true,
			    reader: new Ext.data.JsonReader({
					root: 'products',
					remoteSort: true,
			        fields: ['code','name', {name: 'number', type: 'float'}, 'unit', 'price', {name: 'amount', type: 'float'},'date']
			    }),
	            sortInfo: {field: 'code', direction: 'ASC'},
	            groupField: 'date',
	            
	            listeners: {
	            	beforeload : function() {
	            		//hide the load mask
	        			SALE.Utils.hideLoadMask();
	            	}
	            }
			});
			
			// create the grid
			var csm = new Ext.grid.CheckboxSelectionModel();
			
			// define a custom summary function
		    //Ext.ux.grid.GroupSummary.Calculations['totalCost'] = function(v, record, field){
		       // return v + (record.data.estimate * record.data.rate);
		    //};

			// utilize custom extension for Hybrid Summary
		    var summary = new Ext.grid.GroupSummary();
			
			this.detailsGrid = new Ext.grid.GridPanel({
				store: this.detailsStore,
				//title: '日销售状况',
				columns: [csm,{
					header: "商品编号",
					dataIndex: 'code',
					width: 60,
					summaryRenderer: function(v, params, data){
	                    return ('合计：');
	                },

					sortable: true
				}, {
					header: "商品名称",
					dataIndex: 'name',
					sortable: true
				}, {
					header: "销售数量",
					dataIndex: 'number',
	           		align: 'right',
					width: 80,
					summaryType:'sum',
					sortable: true
				}, {
					header: "单位",
					dataIndex: 'unit',
					width: 45,
					sortable: true
				}, {
					header: "平均单价",
					dataIndex: 'price',
	           		align: 'right',
					sortable: true
				}, {
					header: "销售总额",
					dataIndex: 'amount',					
	           		align: 'right',
	           		summaryType:'sum',
					sortable: true
				}, {
	           		header: "日销售状况统计",
	           		dataIndex: 'date',
	           		width: 70,
	           		align: 'right',
	           		//renderer: Ext.util.Format.dateRenderer('Y-m-d'),
					sortable: true
	        	}],
				
				sm: csm,
				
				tbar:['->',{
            		text: '总体状况',
            		iconCls: 'chart-curve-go',
            		handler: this.showSaleRecentlyDateStatusLineChart.createDelegate(this)
        		},{
            		xtype:'splitbutton',
            		text: '单件状况',
            		iconCls: 'chart-line-link',
            		handler: this.showProductRecentlyDateStatusLineChart.createDelegate(this),
           			menu: [{
						text: '销售趋势',
						iconCls: 'chart-line',
						handler: this.showProductRecentlyDateStatusLineChart.createDelegate(this)
					},{
						text: '销售统计',
						iconCls: 'chart-bar',
						handler: this.showProductRecentlyDateStatusColumnChart.createDelegate(this)
					}]
        		},'-','往日状况：', {
					xtype:'datefield',						
					format:'Y-m-d',
					
					listeners: {
						'select': function(field, date){
        					this.refreshGridByDate(date);							
						}.createDelegate(this)
					}
				}],
				
				plugins: summary,
				
				view: new Ext.grid.GroupingView({
		            forceFit:true,
		            //groupTextTpl: '{text} ({[values.rs.length]} {[values.rs.length > 1 ? "Items" : "Item"]})',
		            showGroupName: false,
		            enableNoGroups:true,
					enableGroupingMenu:false,
		            hideGroupedColumn: true
		        }),
						
				loadMask: true,
				stripeRows: true,
				border: false,
				autoExpandColumn: 'code',
				layout: 'fit',
				
				listeners: {
					cellclick : function(grid, rowIndex, columnIndex, e) {
						this.selectedRowIndex = rowIndex;
					}.createDelegate(this)
				}
			});
		}
		
		if(!SALE.view.chartView) {
			SALE.view.chartView = new SaleChartView();
		}
	},
	
	initlizeDataHandler : function(resultJson) {
		this.detailsJson['products'] = resultJson.products;
		this.detailsStore.load({params:{start:0, limit:25}});
	},
	
	getCorePanel : function() {
		if (!this.detailsGrid) {
			this.initlize();
			SALE.Action.searchSaleDailyStatus(this.initlizeDataHandler.createDelegate(this));
		}
		return this.detailsGrid;
	},
	
	refreshGridByDate : function(date) {
		//show the load mask.
		SALE.Utils.showLoadMask(this.detailsGrid);
		
		SALE.Action.searchSaleDailyStatus(this.initlizeDataHandler.createDelegate(this), date);
	},
	
	showSaleRecentlyDateStatusLineChart : function() {
		
		var title = "近日总体销售状况";
		
		SALE.Action.searchSaleRecentlyDateStatus(this.showLineChartHandler.createDelegate(this), {'title': title});
	},
	
	showProductRecentlyDateStatusLineChart : function() {
		var recordList = this.detailsGrid.getSelectionModel().getSelections();
		if(recordList.length == 0) {
			this.warnMessage('请先选择一条数据！！！', '警告信息');
		} else if(recordList.length > 1) {
			this.warnMessage('每次只能编辑一条数据，请确认只选中了一条数据！！！', '警告信息');
		} else {
			var code = recordList[0].get("code");			
			var title = "近日销售状况";
			SALE.Action.searchProductRecentlyDateStatus(this.showLineChartHandler.createDelegate(this), {'title': title}, code);
		}		
	},
	
	showProductRecentlyDateStatusColumnChart : function() {
		var recordList = this.detailsGrid.getSelectionModel().getSelections();
		if(recordList.length == 0) {
			this.warnMessage('请先选择一条数据！！！', '警告信息');
		} else if(recordList.length > 1) {
			this.warnMessage('每次只能编辑一条数据，请确认只选中了一条数据！！！', '警告信息');
		} else {
			var code = recordList[0].get("code");			
			var title = "近日销售状况";
			SALE.Action.searchProductRecentlyDateStatus(this.showColumnChartHandler.createDelegate(this), {'title': title}, code);			
		}				
	}
});

SaleStatusDailyView.showStatusPanel = function(){
	var  statusView = new SaleStatusDailyView();
	
	SALE.App.showOperateTabPanel(statusView.getCorePanel(), "日销售状况");
}